package cn.strive.eams.busRequire.service.impl;

import cn.strive.eams.application.mapper.ApplicationMapper;
import cn.strive.eams.application.pojo.entity.Application;
import cn.strive.eams.audit.pojo.dto.AuditSaveParam;
import cn.strive.eams.audit.service.AuditService;
import cn.strive.eams.base.enmus.ApplicationStatusEnum;
import cn.strive.eams.busRequire.mapper.BRMapper;
import cn.strive.eams.busRequire.pojo.dto.BusRequirePass;
import cn.strive.eams.busRequire.pojo.dto.BusRequireSaveParam;
import cn.strive.eams.busRequire.pojo.entity.BusRequire;
import cn.strive.eams.busRequire.service.BRService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Service
@Slf4j
public class BRServiceImpl implements BRService {

    @Autowired
    BRMapper busRequireMapper;

    @Autowired
    ApplicationMapper applicationMapper;

    @Autowired
    AuditService auditService;

    @Override
    public void save(BusRequireSaveParam busRequireSaveParam) {
        log.debug("保存校车申请业务需求:busRequireSaveParam={}", busRequireSaveParam);
        BusRequire busRequire = new BusRequire();
        BeanUtils.copyProperties(busRequireSaveParam,busRequire);
        busRequire.setCreateTime(new Date());
        busRequireMapper.insert(busRequire);

        Application application = new Application();
        BeanUtils.copyProperties(busRequireSaveParam,application);
        application.setApplyId(busRequire.getId());
        application.setApplyNumber(generateApplyNumber());
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        log.debug("application={}",application);
        applicationMapper.insert(application);

        AuditSaveParam auditSaveParam = new AuditSaveParam();
        auditSaveParam.setApplyId(busRequire.getId());
        auditSaveParam.setApplicationId(application.getId());
        auditSaveParam.setAuditUserIdList(busRequireSaveParam.getAuditUserIdList());
        auditService.insert(auditSaveParam);

    }

    @Override
    public void insertDriver(BusRequirePass busRequirePass) {
        log.debug("插入司机信息:busRequirePass={}", busRequirePass);
        BusRequire busRequire = new BusRequire();
        busRequire.setId(busRequirePass.getId());
        busRequire.setDriver(busRequirePass.getDriver());
        busRequire.setUpdateTime(new Date());
        busRequireMapper.update(busRequire);

        Application application = new Application();
        application.setId(busRequirePass.getApplyId());
        application.setStatus(ApplicationStatusEnum.DISTRIBUTION.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
    }

    private String generateApplyNumber() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String currentDateStr = LocalDate.now().format(formatter);

        // 初始化四位数字部分，从0000开始
        int numberPart = 0;

        String applyNumber;
        do {
            String formattedNumber = String.format("%04d", numberPart);
            applyNumber = "APPLY" + currentDateStr + formattedNumber;

            // 检查是否有重复的昵称
            if (!isApplyNumberExists(applyNumber)) {
                break;
            }

            // 如果存在重复，则将数字部分增加10
            numberPart += 10;
        } while (true);

        return applyNumber;
    }

    private boolean isApplyNumberExists(String applyNumber) {
        return applicationMapper.existsByApplyNumber(applyNumber) > 0;
    }

}
